package com.comut.crud;

import com.comut.crud.ejb.interfaces.remote.IVehicleActionRemote;
import com.comut.crud.pojo.Response;
import com.comut.crud.pojo.mobile.VehicleDetail;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 * Issue: IFL-1567
 *
 * @author Vitor Borrego
 */
@Path("vehicleDetail/{userId}/{vehicleId}")
public class VehicleDetailResource {

    private final String jndiName = ResourceBundle.getBundle("jndi").getString("vehicleBeanRemote");
    private static final Logger logger = Logger.getLogger(VehicleDetailResource.class.getName());

    @GET
    @Produces("application/json; charset=UTF-8")
    public Response getJson(@PathParam("userId") int userId, @PathParam("vehicleId") int vehicleId) {
        // http://localhost:8080/CRUD-EA-web/resources/VehicleDetail/332/2750
        Response response = new Response();

        try {
            logger.log(Level.INFO, "GET STARTED");
            javax.naming.Context ic = new InitialContext();
            IVehicleActionRemote vehicleAction = (IVehicleActionRemote) ic.lookup(jndiName);
            VehicleDetail vehicleDetail = vehicleAction.getVehicleDetail(userId, vehicleId);
            logger.log(Level.INFO, "GET DONE");
            response.setRetVal(vehicleDetail);
            response.setSuccess(true);
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            response.setSuccess(false);
            response.setMessageError(ex.getMessage());
        }

        return response;
    }
}
